import CreateRoomUI = ui.dialog.CreateRoomUI;
import RadioGroup = Laya.RadioGroup;
import Label = Laya.Label;
import DoudizhuViewUI = ui.game.DoudizhuViewUI;

class GameMain extends ui.game.MainViewUI{
    private DIALOG_WIDTH: number = 1280;
    private DIALOG_HEIGHT: number = 720;
    private CLOSE_BTN_WIDTH: number = 43;
    private CLOSE_BTN_PADDING: number = 5;

    private dialogUi:CreateRoomUI;
    // private pomelo:PomeloLaya.Pomelo;
    // private socket:SocketManager;

    private createParam = {
        doudizhu: {difen: 0, guize: 0, jushu: 0}
    };


    constructor(){
        super();
        var that = this;
        this.doudizhu.on(Laya.Event.CLICK,this, this.onDoudizhuClick)
        this.paodeikai.on(Laya.Event.CLICK,this, this.onPaodeikaiClick)
        // this.socket = new SocketManager();
        // this.pomelo = new PomeloLaya.Pomelo();
        // this.pomelo.on(PomeloLaya.Pomelo.EVENT_IO_ERROR, function(event){
        //     //错误处理
        //     console.error("error",event);

        // });
        // this.pomelo.on(PomeloLaya.Pomelo.EVENT_CLOSE, function(event){
        //     //错误处理
        //     console.error("error",event);
        //     that.reconnectionPomelo();
        // });
        // this.pomelo.on(Laya.Event.OPEN, function(event){
        //     console.log(Laya.Event.OPEN, event)
        // });
    }
    // onDoudizhuClick(){
    //     Laya.loader.load(['res/atlas/doudizhu.atlas'],Handler.create(this, this.DoudizhuLoaded))
    //     console.log("doudizhu")
    // }
    onPaodeikaiClick(){
        console.log("paodeikai")
    }

    onDoudizhuClick(){
        
        this.dialogUi = new CreateRoomUI()
        this.dialogUi.isModal = true;
        this.dialogUi.dragArea = "0,0," + this.DIALOG_WIDTH + "," + this.DIALOG_HEIGHT;
        this.dialogUi.closeBtn.on(Laya.Event.CLICK,this, this.onCloseBtnClick)
        this.dialogUi.createBtn.on(Laya.Event.CLICK,this, this.onCreateBtnClick, ['doudizhu'])
        this.dialogUi.popup();
        this.dialogUi.title.text = "tst"

        var difenText = this.createLabel("底分：", "#cc3300", null).pos(180, 200);
        this.dialogUi.addChild(difenText);

        var gdifenRadio = this.createRadioGroup("1分,5分,10分", "doudizhu", "difen").pos(300,200);
        this.dialogUi.addChild(gdifenRadio);

        var guizeText = this.createLabel("规则：", "#cc3300", null).pos(180, 290);
        this.dialogUi.addChild(guizeText);

        var guizeRadio = this.createRadioGroup("轮流问地主,随机问地主", "doudizhu", "guize").pos(300,290);
        this.dialogUi.addChild(guizeRadio);

        var jushuText = this.createLabel("局数：", "#cc3300", null).pos(180, 380);
        this.dialogUi.addChild(jushuText);

        var jushuRadio = this.createRadioGroup("6局1张房卡,12局2张房卡", "doudizhu", "jushu").pos(300,380);
        this.dialogUi.addChild(jushuRadio);
     
    }

    private createRadioGroup(labels: string, type:string, name:string): RadioGroup {
        var rg: RadioGroup = new RadioGroup();

        rg.skin = "main/radioButton.png";
        rg.space = 30;  
        rg.labels = labels;
        rg.labelColors = "#cc3300,#cc3300,#cc3300";
        rg.labelSize = 30;
        // rg.labelBold = true;
        rg.labelPadding = "5,0,0,5";
        rg.selectHandler = new Handler(this, this.onRadioSelectChange, [type,name]);  
        rg.selectedIndex = 0;
        return rg;
    }
    
    private createLabel(text:string, color: string, strokeColor: string): Label {
        const STROKE_WIDTH: number = 4;

        var lb: Label = new Label();
        lb.font = "Microsoft YaHei";
        lb.text = text;
        lb.fontSize = 30;
        lb.color = color;

        if (strokeColor) {
            lb.stroke = STROKE_WIDTH;
            lb.strokeColor = strokeColor;
        }
        // Laya.stage.addChild(label);

        return lb;
    }
    onRadioSelectChange(type:string, name:string, index:number){
        this.createParam[type][name] = index;
        console.log(name,index,this.createParam)
    }
    onCloseBtnClick(){
        this.dialogUi.close();
    }
    onCreateBtnClick(type:string){
        switch(type){
            case "doudizhu":
                Laya.loader.load(['res/atlas/doudizhu.atlas'],Handler.create(this, function(){
                    var ui = new DoudizhuViewUI();
                    Laya.stage.removeChildren();
                    Laya.stage.addChild(ui);
                    this.joinRoom(type)
                }))
                break;
            case "paodeikuai":
                break;
            default:
            break;
        }
        
    }

    createRoom(){

    }

    joinRoom(type:string){
        // var http = new HttpLaya(function(){

        // });
        // http.sendPost(this.createParam.doudizhu,"");
        var reg = this;   
        // NetworkManager.getInstance().connectPomelo();
        // NetworkManager.getInstance().setPushMsg("@");

        // this.pomelo.init({host:globalConfig.socketUrl, port:30042},(e)=>{
        //     console.log(e);
        //     this.pomelo.notify({"operation":"PrepareJoinRoom","account_id":"6","session":"","data":{"room_number":"100065"}});
        // });
        // this.pomelo.request("@",function(e){
        //     console.log(e)
        // })
        
        // this.pomelo.notify("","@");
        // Laya.timer.loop(5000, this, function(){
        //     reg.pomelo.notify("@");
        // })
        
        // this.pomelo.on(NetworkMgr.PUSH_MSG_JOIN,(event)=>{
        //     console.log("push msg type:",NetworkMgr.PUSH_MSG_JOIN,"data:",event);
        //     NetworkEmitter.fire(NetworkMgr.PUSH_MSG_JOIN,event); 
        // });
                
        
    }
    /**
     * 断开链接
     */
    disconnectPomelo():void{
        // this.pomelo.disconnect();
    }
    /**
     * 重连
     */
    reconnectionPomelo():void{

    }


}
